package com.bw.goods;

import com.bw.goods.dao.SeckillActivityDao;
import com.bw.goods.entity.SeckillActivityEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName StartUpExecute
 * @Author Hao_YunKai
 * @Date 2022/3/10 16:31
 */
@Component
public class StartUpExecute implements ApplicationRunner {
    @Autowired
    private SeckillActivityDao seckillActivityDao;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 项目启动将库存存储到redis
        System.out.println("项目启动将库存存储到redis");
        List<SeckillActivityEntity> list = seckillActivityDao.selectList(null);
        for (SeckillActivityEntity entity : list) {
            redisTemplate.opsForValue().set("SeckillStock_" + entity.getId().toString(),entity.getStockCount().toString());
        }
    }
}
